<!-- 正文开始 -->
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <!-- 头部操作栏 -->
            <div class="layui-form lay-header">
                <div class="toolbar">
                    <div class="layui-form-item lay-query-content">
                        <div class="lay-query-box">
                            <div class="query-lt">
                 <!--               <div class="layui-inline">
                                    <label class="layui-form-label w-auto">类型：</label>
                                    <div class="layui-input-inline">
                                        <select name="" lay-filter="">
                                            <option value="">类型</option>
                                            <option value="1">柜子</option>
                                            <option value="2">工具柜</option>
                                        </select>
                                    </div>
                                </div>-->
               <!--                 <div class="layui-inline">
                                    <label class="layui-form-label w-auto">型号：</label>
                                    <div class="layui-input-inline">
                                        <select name="" lay-filter="">
                                            <option value="">型号</option>
                                            <option value="1">YD001Tbox主柜</option>
                                            <option value="2">YD001Tbox售刀副柜</option>
                                            <option value="3">YD001Tbox暂存副柜</option>
                                        </select>
                                    </div>
                                </div>-->

                                <div class="layui-inline">
                                    <label class="layui-form-label w-auto">配货订单编号：</label>
                                    <div class="layui-input-inline">
                                        <input name="deliveryOrderCode" class="layui-input" type="text" placeholder="请输入"/>
                                    </div>
                                </div>

                        <!--        <div class="layui-inline">
                                    <label class="layui-form-label w-auto">状态：</label>
                                    <div class="layui-input-inline">
                                        <select name="status" lay-filter="">
                                            <option value="">状态</option>
                                            <option value="0">已创建</option>
                                            <option value="3">配送中</option>
                                            <option value="5">已签收</option>
                                        </select>
                                    </div>
                                </div>-->
                  <!--              <div class="layui-inline">
                                    <label class="layui-form-label w-auto">订单编号：</label>
                                    <div class="layui-input-inline">
                                        <input name="code" class="layui-input" type="text" placeholder="请输入订单编号"/>
                                    </div>
                                </div>
                                <div class="layui-inline">
                                    <label class="layui-form-label w-auto">客户名称：</label>
                                    <div class="layui-input-inline">
                                        <input name="customerName" class="layui-input" type="text" placeholder="请输入客户名称"/>
                                    </div>
                                </div>-->
                       <!--         <div class="layui-inline">
                                    <label class="layui-form-label w-auto">领货时间：</label>
                                    <div class="layui-input-inline">
                                        <input name="createTime" id="pTime" class="layui-input date-icon" style="width: 200px;" type="text" placeholder="选择创建时间" autocomplete="off"/>
                                    </div>
                                    <input name="startCreateTime" id="sTime" type="hidden"/>
                                    <input name="endCreateTime" id="eTime" type="hidden"/>
                                </div>-->
                            </div>
                            <div class="query-rt">
                                <div class="layui-inline">
                                    <button class="layui-btn icon-btn color-green" lay-filter="cloudDistributionSearch" lay-submit>查询</button>
                                    <button class="layui-btn icon-btn color-white" lay-filter="resets" lay-submit>重置</button>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <!-- 表格 -->
            <table class="layui-table" id="cloudDistributionTable" lay-filter="cloudDistributionTable"></table>
        </div>
    </div>
</div>

<!-- 表格操作列 -->
<script type="text/html" id="cloudDistributionTableBar">
    <a class="layui-btn layui-btn-xs" lay-event="detailsBtn">详情</a>
</script>

<!-- 详情弹窗 -->
<script type="text/html" id="detailsForm">
    <div class="layui-fluid">
        <div class="layui-card">
            <div class="layui-card-body">
                <form class="layui-form" action="" lay-filter="detailsForm">
                    <div class="layui-form-item">
                        <table class="layui-table" id="detailsFormTable" name="detailsFormTable" lay-filter="detailsFormTable"></table>
                    </div>
                    <div class="layui-form-item">
                        <table class="layui-table" id="distributionDetailsFormTable" name="distributionDetailsFormTable" lay-filter="distributionDetailsFormTable"></table>
                    </div>
                </form>
            </div>
        </div>
    </div>
</script>


<!-- 配货详情弹窗 -->
<!-- distributionDetailsForm -->
<script type="text/html" id="distributionDetailsForm">
    <div class="layui-fluid">
        <div class="layui-card">
            <div class="layui-card-body">
                <form class="layui-form" action="" lay-filter="distributionDetailsForm">
                    <div class="layui-form-item">
                        <table class="layui-table" id="distributionDetailsTableOne" name="distributionDetailsTableOne" lay-filter="distributionDetailsTableOne"></table>
                    </div>
                    <div class="layui-form-item">
                        <table class="layui-table" id="distributionDetailsTableTwo" name="distributionDetailsTableTwo" lay-filter="distributionDetailsTableTwo"></table>
                    </div>
                </form>
            </div>
        </div>
    </div>
</script>



<!-- js部分 -->
<script>
    layui.use(['layer', 'form', 'table', 'admin', 'laydate'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var config = layui.config;
        var admin = layui.admin;
        var upload = layui.upload;
        var laydate = layui.laydate;
        var deliveryList = []; //配送详情
        var distributionDetailList = []; //配货明细详情


        // 渲染表格
        var insTb = table.render({
            elem: '#cloudDistributionTable',
            url: config.base + 'enterprisecab/order/admin/cloudTakeDelivery/getOrderDeliveryList?sort=create_time&order=asc',
            page: true,
            cellMinWidth: 100,
            cols: [[
                // {type: 'checkbox'}
                {field: 'number', title: '序号', width: 50, type: 'numbers' }
                // ,{field: 'receiveCode',  title: '领货记录单'}
                ,{field: 'deliveryOrderCode',  title: '配货订单'}
                ,{field: 'skuNum',  title: '物料数量'}
                ,{field: 'warhouseName', title: '出库仓库'}
                ,{field: 'addr', title: '出库所在地区'}
                ,{field: 'companyName',  title: '所属服务商'}
                ,{field: 'createTime', title: '出库时间'}
                ,{field: 'statusDesc',  title: '状态'}
                ,{field: 'claimByName',  title: '配货员'}
                ,{align: 'center', toolbar: '#cloudDistributionTableBar', title: '操作'}
            ]],           
        });

        //渲染下拉框
        form.render('select');

        // 搜索
        form.on('submit(cloudDistributionSearch)', function (data) {
            insTb.reload({where: data.field,page: {curr: 1}},'data');
        });

        // 工具条点击事件
        table.on('tool(cloudDistributionTable)', function (obj) {
            var data = obj.data;
            if (obj.event === 'detailsBtn') { //详情
                showDetailsForm(data)
            } 
        });

        //时间范围
        laydate.render({
            elem: 'input[name="createTime"]',
            type: 'date',
            range: true,
            done: function(value, date, endDate){
                var dateValue = value.split(" - ");
                $("input[name='startCreateTime']").val(dateValue[0]);
                $("input[name='endCreateTime']").val(dateValue[1]);
            }
        });

        //监听排序
        table.on('sort(cloudDistributionTable)', function(obj){
            table.reload('cloudDistributionTable', {
                initSort: obj
                ,where: {
                    sort: obj.field
                    ,order: obj.type
                }
            });
        });

        // 显示详情
        // detailsForm
        function showDetailsForm(_data){
            console.log(_data)
            admin.open({
                type: 1,
                area: ['1450px','800px'],
                offset: '65px',
                title: '详情',
                content: $('#detailsForm').html(),
                success: function () {
                    // 渲染表格
                    var detailsFormTb = table.render({
                        elem: '#detailsFormTable',
                         url: config.base + 'enterprisecab/order/admin/cloudTakeDelivery/getOrderDeliveryList?sort=create_time&order=asc&deliveryOrderCode='+_data.deliveryOrderCode,
                        data:[_data],
                        cellMinWidth: 100,
                        cols: [[
                             {field: 'deliveryOrderCode', title: '配送领用记录编号'}
                            ,{field: 'distributionCode', title: '配货任务编号'}
                            ,{field: 'distributionCode', title: '所属配货订单'}
                            ,{field: 'warhouseName', title: '所属仓库'}
                            ,{field: 'skuNum', title: '物料数'}
                            ,{field: 'distributionValue', title: '订单货值'}
                            ,{field: 'statusDesc', title: '状态'}
                            ,{field: 'claimByName', title: '配送员'}
                            ,{field: 'claimTime', title: '认领时间'}
                            ,{field: 'departureTime', title: '配送时间'}
                        ]],           
                    });

                    //配送明细
                    for (var i = 0; i <_data.deliveryDetails.length ; i++) {
                        deliveryList[i] = _data.deliveryDetails[i]
                        table.reload('distributionDetailsFormTable',{
                            data: deliveryList[i],
                        })
                        console.log(_data.deliveryDetails[i])
                    }

                    //TODO newMode
                    var distributionDetailsFormTb = table.render({
                        elem: '#distributionDetailsFormTable',
                        // url: config.base + 'enterprisecab/order/admin/cloudTakeDelivery/getOrderDeliveryDetailList?sort=create_time&order=asc&deliveryId='+_data.id,
                        data: deliveryList,
                        cellMinWidth: 100,
                        cols: [[
                            {field: 'number', title: '序号', width: 50, type: 'numbers' }
                            ,{field: 'skuCode', title: '物料编号'}
                            ,{field: 'productName', title: '物料系列名称'}
                            ,{field: 'skuSpecValueNames', title: '规格'}  // 有两个物料规格 productSpecName productSpecCode（下同）
                            ,{field: 'unitName', title: '单位'}
                            ,{field: 'statusDesc', title: '状态'}
                            ,{field: 'quantityDistribute', title: '实配数'}
                            ,{field: 'quantityReceive', title: '领取数'}
                            ,{align: 'center', toolbar: '#cloudDistributionTableBar', title: '操作'}
                        ]],
                    });
  /*                  console.log("**"+_data.unitName)
                    console.log(_data.statusDesc)
                    console.log(_data.quantityDistribute)*/
                    // 工具条点击事件
                    table.on('tool(distributionDetailsFormTable)', function (obj) {
                        var data = obj.data;
                            data.deliveryTaskSukNum = _data.deliveryTaskSukNum
                            data.demandCode = _data.demandCode
                            data.distUser = _data.distUser
                            data.confirmTime = _data.confirmTime
                            data.distId = _data.id
                            data.claimByName = _data.claimByName
                            data.claimTime = _data.claimTime
                            data.distributionCode = _data.distributionCode
                            // data.unitName = _data.unitName


                        if (obj.event === 'detailsBtn') { //详情
                            showDistributionDetailsForm(data)
                        } 
                    });
                }
            })

        }


        // 配送详情
        // distributionDetailsForm
        function showDistributionDetailsForm(_data){
            console.log(_data)
            admin.open({
                type: 1,
                area: ['1400px','700px'],
                offset: '85px',
                title: '配送详情',
                content: $('#distributionDetailsForm').html(),
                success: function () {
                    // 渲染表格
                    var distributionDetailsTableTb1 = table.render({
                        elem: '#distributionDetailsTableOne',
                        // url: config.base + 'enterprisecab/order/admin/cloudTakeDelivery/getOrderDeliveryDetailList?sort=create_time&order=asc&deliveryId='+_data.id,
                        data:[_data],
                        cellMinWidth: 100,
                        cols: [[
                            {field: 'distributionCode', title: '配货任务编号'}
                            ,{field: 'distributionCode', title: '所属配货订单'}
                            ,{field: 'skuCode', title: '物料编号'}
                            ,{field: 'productName', title: '物料系列名称'}
                            ,{field: 'skuSpecValueNames', title: '规格'}
                            ,{field: 'unitName', title: '单位'}
                            ,{field: 'curentStock', title: '仓库库存'}
                            ,{field: 'quantityConfirm', title: '补货数'}
                            ,{field: 'quantityDistribute', title: '配货数'}
                            ,{field: 'quantityReceive', title: '已认领数'}
                            ,{field: 'claimByName', title: '配送员'}
                            ,{field: 'claimTime', title: '认领时间'}

                        ]],           
                    });

                    admin.req('enterprisecab/order/admin/cloudTakeDelivery/getOrderDeliveryDetailList',{deliveryId:_data.deliveryId,id:_data.id},function (res) {
                        if (res.code==200){
                            // distributionDetailList = res.data;
                            //配送明细
                            for (let i = 0; i <res.data.length; i++) {
                                distributionDetailList[i] = res.data[i].orderDistributionLabels

                            table.reload('distributionDetailsTableTwo',{
                                data: distributionDetailList[i],
                            })
                            }
                        }
                    },'GET')
                    var distributionDetailsTableTb2 = table.render({
                        elem: '#distributionDetailsTableTwo',
                        data: distributionDetailList,
                        // url: config.base + 'enterprisecab/order/admin/cloudTakeDelivery/getOrderDeliveryDetailList?sort=create_time&order=asc&deliveryId='+_data.id,
                        cellMinWidth: 100,
                        cols: [[
                            {field: 'number', title: '序号', width: 50, type: 'numbers'}
                            ,{field: 'packLabelCode', title: '包装编码'}
                            ,{field: 'productName', title: '物料系列名称'}
                            ,{field: 'packSpecCode', title: '包装规格'}
                            ,{field: 'unitName', title: '单位'}
                            ,{field: 'baseUnitNum', title: '最小单位数'}
                            ,{field: 'baseUnitName', title: '最小单位'}
                            ,{field: 'packLabelQuantity', title: '配货数(最小单位:支)'}
                            ,{field: 'quantityReceive', title: '已领取数(按最小单位'}
                        ]],
                    });
                }
            })
        }
    });
</script>